package cn.smxy.category.controller;

import cn.smxy.category.service.PlantFamilyService;
import cn.smxy.dto.FamilyDto;
import cn.smxy.param.FamilyFuzzyParam;
import cn.smxy.param.FamilyIdsParam;
import cn.smxy.pojo.PlantFamily;
import cn.smxy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category/family")
public class PlantFamilyController {

    @Autowired
    private PlantFamilyService plantFamilyService;

    /**
     * 返回科属树形目录接口
     *
     * @param categoryId 种群id
     * @return
     */
    @GetMapping("/directory/{categoryId}")
    public R directory(@PathVariable("categoryId") Long categoryId) {
        return plantFamilyService.directory(categoryId);
    }

    /**
     * 返回科名列表接口
     *
     * @param categoryId 种群id
     * @return
     */
    @GetMapping("/show/{categoryId}")
    public R show(@PathVariable("categoryId") Long categoryId) {
        return plantFamilyService.show(categoryId);
    }

    /**
     * 添加科名接口
     *
     * @param plantFamily 科属实体类
     * @param result
     * @return
     */
    @PostMapping("/add")
    public R addFamily(@RequestBody PlantFamily plantFamily, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("添加失败");
        }
        return plantFamilyService.addFamily(plantFamily);
    }

    /**
     * 添加属名接口
     *
     * @param plantFamily 科属实体类
     * @param result
     * @return
     */
    @PostMapping("/add/genus")
    public R addGenus(@RequestBody PlantFamily plantFamily, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("添加失败");
        }
        return plantFamilyService.addGenus(plantFamily);
    }

    /**
     * 返回科属全称字符串接口
     *
     * @param familyId 科属id
     * @return 科属拼接全称字符串
     */
    @GetMapping("/full/{familyId}")
    public FamilyDto full(@PathVariable Long familyId) {
        return plantFamilyService.full(familyId);
    }

    /**
     * 查询科属id的所有子集id集合接口
     *
     * @param familyIdsParam 科属id集合
     * @return 科属id集合
     */
    @PostMapping("/list")
    public List<Long> list(@RequestBody FamilyIdsParam familyIdsParam) {
        return plantFamilyService.list(familyIdsParam);
    }

    // TODO:管理员模块

    /**
     * 按条件分页查询科属接口
     *
     * @param familyFuzzyParam 种群id 科属id 分页参数 模糊查询科属名
     * @return
     */
    @PostMapping("/fuzzy")
    public R fuzzy(@RequestBody FamilyFuzzyParam familyFuzzyParam) {
        return plantFamilyService.fuzzy(familyFuzzyParam);
    }

    /**
     * 删除科属接口
     *
     * @param familyId
     * @return
     */
    @DeleteMapping("/remove/{familyId}")
    public R familyRemove(@PathVariable Long familyId) {
        return plantFamilyService.familyRemove(familyId);
    }

    /**
     * 返回属名列表接口
     *
     * @param familyId 科属id
     * @return
     */
    @GetMapping("/show/genus/{familyId}")
    public R genusShow(@PathVariable("familyId") Long familyId) {
        return plantFamilyService.genusShow(familyId);
    }
}
